menuList[2].onclick();
// 获取数据并渲染
current=1;
pageSize=10;
let result;
let searchContent = -1;
let flagData=1;//判断当前分页数据是那些类型 1、所有数据 2、搜索结果
async function fun(){
    console.log(flagData);
    let checkAll = document.querySelector("#checkAll");
    checkAll.checked = false;
    dels=[];
    if(flagData==1){
        result = await getData(current,pageSize);
        counts = result.data.totalCounts;
        console.log(counts);
        pages = result.data.totalPage;
        pageFun({
            current:current,
            pages:pages,
            counts:counts,
            pageSize:pageSize
        })
    }else if(flagData==2){
        searchByUsername()
    }else if(flagData==3){
        result = await getStateFun(current,pageSize);
        counts = result.data.totalCounts;
        console.log(counts);
        pages = result.data.totalPage;
        pageFun({
            current:current,
            pages:pages,
            counts:counts,
            pageSize:pageSize
        })
    }
   
}
fun();
// 获取数据
function getData(current,pageSize){
    return new Promise((resolve, reject) => {
        axios({
            url:'/admin/goods/ManageAllGoods',
            method:'GET',
            params:{
                page:current,
                size:pageSize
                }
            })
            .then(data => {
                console.log(data.data);
                decorateFun(data)
                resolve(data.data);
            })
            .catch(err => {
                console.log(err);
            })
    });
}

// 
function closeOrderWin(){
    let covers = cl("covers");
    let alertWindow = cl("alertWindow");
    let reasonWin = cl("reasonWin");
    covers.style.display ="none";
    alertWindow.style.display ="none";
    reasonWin.style.display ="none";
}
// 批量删除
function openDeleteGoodsAllFun(){
    console.log(dels);
    if(dels.length==0){
        explain({
            code:3,
            msg:"请选择需要删除的商品",
            time:3000
        })
    }else{
        pointFun({
            code:9,
            msg:"是否确认删除选中的商品"
        })
    }
}
function deleteGoodsAllFun(){
    axios({
        url:'/admin/goods/deleteGoods',
        method:'DELETE',
        params:{
            ids:dels
        }
    })
    .then(data => {
        console.log(data);
        if(data.data.code==20000){
            closeOrderWin();
            fun();
            explain({
                code:1,
                msg:"删除成功",
                time:3000
            })
        }
    })
    .catch(err => {
        console.log(err);
    })
}
// 关闭函数
function closeGoodsWin(){
    let covers = cl("covers");
    let alertWindow = cl("alertWindow");
    covers.style.display ="none";
    alertWindow.style.display ="none";
}
// 删除单个订单
let goodsid;
let userid;
function openDeleteGoodsFun(e){
    e=e || window.event;
    var target=e.target || e.srcElement;
    goodsid = target.getAttribute("name")
    console.log(e.target);
    pointFun({
        code:8,
        msg:"是否删除该商品"
    })
    console.warn(target.getAttribute('name'));
}
function deleteGoodsFun(){
    axios({
        url:'/admin/goods/deleteAGood',
        method:'DELETE',
        params:{
            goodsId:goodsid
        }
    })
    .then(data => {
        console.log(data);
        if(data.data.code==20000){
            closeOrderWin();
            fun();
            explain({
                code:1,
                msg:"删除成功",
                time:3000
            })
        }
    })
    .catch(err => {
        console.log(err);
    })
}

// 搜索通过订单号
let searchInput = cl("searchInput");
function searchByUsername(){
    if(searchContent != searchInput.value){    
        page=1;
        pageSize=10;
        current=1;
    } 
    console.warn(searchContent==searchInput.value);
    axios({
        url: '/admin/goods/findGoodsByName',
        method: 'GET',
        params:{
            goodsName : searchInput.value,
            page: current,
            size: pageSize,
            } 
        })
        .then(data => {
            if(data.data.code==20001){
                explain({
                    code:2,
                    msg:"抱歉未找到",
                    time:3000
                })
            }else{
                searchContent = searchInput.value
                flagData=2;
                result = data.data.data;
                counts = result.totalCounts;
                pages = result.totalPage
                console.log(result);
                decorateFun(data)
                pageFun({
                    current:current,
                    pages:result.totalPage,
                    counts:result.totalCounts,
                    pageSize:result.size
                })
            }
        })
        .catch(err => {
            console.log(err);
        })
}

// 渲染页面
function decorateFun(data){
    let tbodyMenu = cl("tbodyMenu");
    let dataNums = cl("dataNums");
    let pageMessage = cl("pageMessage");
    let end;
    if(current==1){
        end=data.data.data.list.length;
    }else{
        end =parseInt(current-1)*parseInt(data.data.data.size)+parseInt(data.data.data.list.length);
    }
    dataNums.innerHTML = data.data.data.list.length;
    pageMessage.innerHTML = `第${(current-1)*data.data.data.size+1}到${end}条记录，共${data.data.data.totalCounts}条`;
    tbodyMenu.innerHTML ="";
    let i=0;
    for(let key of data.data.data.list){
        tbodyMenu.innerHTML += `
        <tr>
            <td>
                <input type="checkbox" name="${key.goodsId}" class="checkbox" onclick="checkboxFun()">
            </td>
            <td>${key.goodsName}</td>
            <td>${handlePrice(key.goodsPrice)}</td>
            <td>${key.monthlySales}</td>
            <td>${key.flag==0?"未审核":"已审核"}</td>
            <td>${key.flag==1?"已启用":"已下架"}</td>
            <td>
                <button class="btn ${key.flag==1?"useBtn":"noUseBtn"}" flag="${key.flag}" name="${key.goodsId}" userId="${key.userId}" onclick="openPassGoodsFun()">
                    <i class=" iconfont htmal5icon22" name="${key.goodsId}" flag="${key.flag}" userId="${key.userId}" ></i>
                </button>
                <a href="/admin/goods/goodsDetail?goodsId=${key.goodsId}&userId=${key.userId}">
                    <button class="btn editBtn">
                        <i class="iconfont bianji"></i>
                    </button>
                </a>
                <button class="btn delBtn" name="${key.goodsId}" userId="${key.userId}" onclick="openDeleteGoodsFun()">
                    <i class="iconfont shanchu" name="${key.goodsId}"></i>
                </button>
            </td>
        </tr>
            `
            i++
        }
}
// 输入框回车事件
function inputEnterFun(){
    if(event.keyCode == 13){
        searchByUsername()
    }
}
// 根据用户的状态进行筛选
function chooseFlagFun(){
    let selectFlag = document.getElementById("selectFlag");
    if(selectFlag.value==-1){
        flagData=1;
    }else{
        flagData=3;
    }
    current = 1;
    pageSize =10;
    fun();
}
function getStateFun(){
    let selectFlag = document.getElementById("selectFlag");
    console.warn(selectFlag.value==-1);
    if(selectFlag.value==-1){
        flagData=1;
        fun();
    }else{
        return new Promise((resolve, reject) => {
            axios({
                url:'/admin/goods/findGoodsByFlag',
                method:'GET',
                params:{
                    page:current,
                    size:pageSize,
                    flag:selectFlag.value
                    }
                })
                .then(data => {
                    if(data.data.code==20000){
                        decorateFun(data)
                        console.log(data.data);
                        resolve(data.data);
                    }else{
                        explain({
                            code:3,
                            msg:"当前没有未上架的商品",
                            time:3000
                        })
                        pointFun({
                            code:10,
                             msg:"暂无未审核的商品"
                        })
                        location.reload();
                    }
                })
                .catch(err => {
                    console.log(err);
                })
        });
    }
}
// 页面刷新
function reloadFun(){
    location.reload();
}

// 选择每页展示数量
function chooseSizeFun(){
    let selectNums = document.getElementById("selectNums");
    console.warn(selectNums.value);
    current = 1;
    pageSize =selectNums.value;
    console.warn(pageSize);
    fun();
}
//询问是否通过或下架一个商品
function openPassGoodsFun(e){
    e=e || window.event;
    var target=e.target || e.srcElement;
    goodsid = target.getAttribute("name")
    userid = target.getAttribute("userId")
    let flag = target.getAttribute("flag");
    if(flag!=1){
        pointFun({
            code:10,
            msg:"是否通过该商品的审核"
        })
    }else if(flag==1){
        let reasonWin = cl("reasonWin");
        let covers = cl("covers");
        reasonWin.style.display="block"
        covers.style.display="block"
    }
    console.log(e.target);
    console.warn(target.getAttribute('name'));
} 
function alertInputs(){
    var str = prompt("请输入拒绝理由");
    if(str){
        alert(str);
    }
}
// alertInputs();
// prompt("请输入拒绝理由");
//通过一个商品
function passGoodsFun(){
    axios({
        url:'/admin/goods/passAGood',
        method:'PATCH',
        params:{
            goodsId:goodsid
        }
    })
    .then(data => {
        console.log(data);
        if(data.data.code==20000){
            closeOrderWin();
            fun();
            messageInitFun()
            explain({
                code:1,
                msg:"通过审核成功",
                time:3000
            })
        }
    })
    .catch(err => {
        console.log(err);
    })
}
// 打开商品下架理由面板
function openUndercarriageGoodsFun(e){
    e=e || window.event;
    var target=e.target || e.srcElement;
    goodsid = target.getAttribute("name")
    userid = target.getAttribute("userId")
    console.log(userid);
    let flag = target.getAttribute("flag");
    if(flag==1){
        pointFun({
            code:10,
            msg:"是否通过该商品的审核"
        })
    }
}
// 下架一个商品
function undercarriageGoodsFun(){
    let reasonWrap = cl("reasonWrap");
    console.log(reasonWrap.value);
    console.log(userid);
    axios({
        url:'/admin/goods/refuseAGood',
        method:'POST',
        params:{
            goodsId:goodsid,
            comment :reasonWrap.value,
            userId :userid 
        }
    })
    .then(data => {
        console.log(data);
        if(data.data.code==20000){
            closeOrderWin();
            fun();
            explain({
                code:1,
                msg:"下架成功成功",
                time:3000
            })
        }
    })
    .catch(err => {
        console.log(err);
    })
}
function closeFunUser(){
    let reasonWin = cl("reasonWin");
    let covers = cl("covers");
    reasonWin.style.display="none"
    covers.style.display="none"
}
